{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'xlrd'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-70e5e47dcb4c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpandas\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mwarnings\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mxlrd\u001b[0m \u001b[0;31m#读取excel的库\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0mresArray\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m#先声明一个空list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'xlrd'"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import warnings\n",
    "import xlrd #读取excel的库\n",
    "\n",
    "resArray=[] #先声明一个空list\n",
    "data = xlrd.open_workbook(\"/Users/fangzhou/Desktop/CFQ4.xlsx\") #读取文件\n",
    "table = data.sheet_by_index(3) #按索引获取工作表，0就是工作表1\n",
    "for i in range(table.nrows): #table.nrows表示总行数\n",
    "    line=table.row_values(i) #读取每行数据，保存在line里面，line是list\n",
    "    resArray.append(line) #将line加入到resArray中，resArray是二维list\n",
    "\n",
    "class AHP:\n",
    "    def __init__(self, criteria):\n",
    "        self.RI = (1/9, 1/7, 1/5, 1/3, 1, 3, 5, 7, 9)\n",
    "        self.criteria = criteria\n",
    "        self.num_criteria = criteria.shape[0]\n",
    "\n",
    "    def cal_weights(self, input_matrix):\n",
    "        input_matrix = np.array(input_matrix)\n",
    "        n, n1 = input_matrix.shape\n",
    "        assert n == n1, '不是一个方阵'\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if np.abs(input_matrix[i, j] * input_matrix[j, i] - 1) > 1e-7:\n",
    "                    print(i,j)\n",
    "                    raise ValueError('不是反互对称矩阵')\n",
    "                    \n",
    "\n",
    "        eigenvalues, eigenvectors = np.linalg.eig(input_matrix)\n",
    "\n",
    "        max_idx = np.argmax(eigenvalues)\n",
    "        max_eigen = eigenvalues[max_idx].real\n",
    "        eigen = eigenvectors[:, max_idx].real\n",
    "        eigen = eigen / eigen.sum()\n",
    "\n",
    "        CI = (max_eigen - n) /(n - 1)\n",
    "        CR = CI / self.RI[len(self.RI)-1]\n",
    "        \n",
    "        return max_eigen, CR, eigen\n",
    "\n",
    "    def run(self):\n",
    "        max_eigen, CR, criteria_eigen = self.cal_weights(self.criteria)\n",
    "        print('准则层权重={}\\n'.format(criteria_eigen))\n",
    "        \n",
    "        \n",
    "if __name__ == '__main__':\n",
    "    # 准则比较矩阵\n",
    "    criteria = np.array(resArray)\n",
    "    print(criteria)\n",
    "    a = AHP(criteria).run()\n",
    "                        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
